import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.apache.commons.collections.CollectionUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;


public class BuildCodeAction extends AnAction {

    private static String txt = ".txt";
    private static String java = ".java";
    //代码所在路径
    private static String commonBaseUrl = "D:\\code\\luffy.gaodunwangxiao.com\\";
    //common包代码所在根路径
    private static String resultCommonBaseUrl = commonBaseUrl + "luffy-common\\src\\main\\java\\com\\gaodun\\luffy\\";
    //api包代码所在根路径
    private static String resultApiBaseUrl = commonBaseUrl + "luffy-api\\src\\main\\java\\com\\gaodun\\luffy\\";
    //基础包名
    private static String resultBasePackage = "com.gaodun.luffy.";
    //导入基础包名
    private static String baseImportPackage = "import " + resultBasePackage;


    private static String cnName = "主类";
    private static String enName = "CreateDemo";
    private static String packageName = "fengyamin";


    private static String detailCnName = "子类详情";
    private static String detailEnName = "CreateDemoDetail";


    @Override
    public void actionPerformed(AnActionEvent e) {
        // 创建对话框，并监听数据
        SubmitConfigDialog dialog = new SubmitConfigDialog(e.getProject(), "生成代码配置");
        dialog.show();
    }


    public static void buildFile(String commonBaseUrlText, String cnNameText, String enNameText, String packageNameText, String detailCnNameText, String detailEnNameText, String buildDetailFlagText) {
        commonBaseUrl = commonBaseUrlText;
        cnName = cnNameText;
        enName = enNameText;
        packageName = packageNameText;
        if ("是".equals(buildDetailFlagText)) {
            detailCnName = detailCnNameText;
            detailEnName = detailEnNameText;
        }

        createFile(false, "DTO", resultBasePackage + "service." + packageName + ".dto", resultCommonBaseUrl + "service\\" + packageName + "\\dto");
        createFile(false, "WebVO", resultBasePackage + "service." + packageName + ".vo", resultCommonBaseUrl + "service\\" + packageName + "\\vo");
        createFile(false, "Handler", resultBasePackage + "domain.scrm.handler." + packageName, resultCommonBaseUrl + "domain\\scrm\\handler\\" + packageName);

        createFile(false, "Service", resultBasePackage + "service." + packageName, resultCommonBaseUrl + "service\\" + packageName);
        createFile(false, "ServiceImpl", resultBasePackage + "service." + packageName + ".impl", resultCommonBaseUrl + "service\\" + packageName + "\\impl");

        createFile(false, "Endpoint", resultBasePackage + "web." + packageName, resultApiBaseUrl + "web\\" + packageName);

        if ("是".equals(buildDetailFlagText)) {
            createFile(true, "DTO", resultBasePackage + "service." + packageName + ".dto", resultCommonBaseUrl + "service\\" + packageName + "\\dto");
            createFile(true, "Handler", resultBasePackage + "domain.scrm.handler." + packageName, resultCommonBaseUrl + "domain\\scrm\\handler\\" + packageName);
        }
        System.out.println("生成成功");
    }


    /**
     * 生成模板代码
     */
    private static void createFile(Boolean isDetail, String type, String packageUrl, String pathUrl) {
        FileUtil.mkdir(pathUrl);
        InputStream templateStream = BuildCodeAction.class.getClassLoader().getResourceAsStream("template/CreateDemo" + (isDetail ? "Detail" : "") + type + txt);
        List<String> strings = new ArrayList<>();
        BufferedReader reader = null;
        try {
            reader = IoUtil.getReader(templateStream, StandardCharsets.UTF_8);
            String line;
            while (true) {
                line = reader.readLine();
                if (line == null) {
                    break;
                }
                strings.add(line);
            }
        } catch (IOException e) {
            throw new IORuntimeException(e);
        } finally {
            IoUtil.close(reader);
        }

        if (!CollectionUtils.isEmpty(strings)) {
            StringBuffer stringBuffer = new StringBuffer();
            for (String str : strings) {
                stringBuffer.append(str + "\n");
            }

            String text = stringBuffer.toString();
            String result = text.replaceAll("CreateDemoDetail", detailEnName)
                    .replaceAll("createDemoDetail", StrUtil.lowerFirst(detailEnName))
                    .replaceAll("CreateDemo", enName)
                    .replaceAll("createDemo", StrUtil.lowerFirst(enName))
                    .replaceAll("创建示例明细", detailCnName)
                    .replaceAll("创建示例", cnName)
                    .replaceAll(baseImportPackage + "template." + enName + "Service", baseImportPackage + "service." + packageName + "." + enName + "Service")
                    .replaceAll(baseImportPackage + "template." + detailEnName + "Handler", baseImportPackage + "domain.scrm.handler." + packageName + "." + detailEnName + "Handler")
                    .replaceAll(baseImportPackage + "template." + enName + "Handler", baseImportPackage + "domain.scrm.handler." + packageName + "." + enName + "Handler")
                    .replaceAll(baseImportPackage + "template." + detailEnName + "DTO", baseImportPackage + "service." + packageName + ".dto." + detailEnName + "DTO")
                    .replaceAll(baseImportPackage + "template." + detailEnName + "WebVO", baseImportPackage + "service." + packageName + ".vo." + detailEnName + "WebVO")
                    .replaceAll(baseImportPackage + "template." + enName + "DTO", baseImportPackage + "service." + packageName + ".dto." + enName + "DTO")
                    .replaceAll(baseImportPackage + "template." + enName + "WebVO", baseImportPackage + "service." + packageName + ".vo." + enName + "WebVO")
                    .replaceAll(baseImportPackage + "template.done." + detailEnName + "Mapper", baseImportPackage + "domain.scrm.repository." + detailEnName + "Mapper")
                    .replaceAll(baseImportPackage + "template.done." + detailEnName, baseImportPackage + "domain.scrm.entity." + detailEnName)
                    .replaceAll(baseImportPackage + "template.done." + enName + "Mapper", baseImportPackage + "domain.scrm.repository." + enName + "Mapper")
                    .replaceAll(baseImportPackage + "template.done." + enName, baseImportPackage + "domain.scrm.entity." + enName)
                    .replaceAll("package com.gaodun.luffy.template;", "package " + packageUrl + ";");
            if (isDetail) {
                FileUtil.writeUtf8String(result, pathUrl + "\\" + detailEnName + type + java);
            } else {
                FileUtil.writeUtf8String(result, pathUrl + "\\" + enName + type + java);
            }
        }
    }


}
